package by.dezhits.liltweets.fragment;

import by.dezhits.liltweets.ContextHolder;
import by.dezhits.liltweets.R;
import by.dezhits.liltweets.WebActivity;
import by.dezhits.liltweets.adapter.TweetCursorAdapter;
import by.dezhits.liltweets.api.TwitterAPI;
import by.dezhits.liltweets.content.contract.TweetContract;
import by.dezhits.liltweets.http.HttpRequestBuilder;
import by.dezhits.liltweets.notifications.NotificationsHelper;
import by.dezhits.liltweets.service.HttpService;
import android.app.AlertDialog;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;

public class SearchResultTweetsFragment extends ListFragment {
	
	private TweetCursorAdapter tweetCursorAdapter;

	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = super.onCreateView(inflater, container, savedInstanceState);
		tweetCursorAdapter = new TweetCursorAdapter(this.getActivity(), null);
		this.setListAdapter(tweetCursorAdapter);
		getLoaderManager().initLoader(0, null, new LoaderCallbacks<Cursor>() {

			@Override
			public Loader<Cursor> onCreateLoader(int paramInt,
					Bundle paramBundle) {
				return new CursorLoader(SearchResultTweetsFragment.this.getActivity(),
						TweetContract.TweetsColumns.SEARCH_RESULT_URI, null, null,
						null, TweetContract.TweetsColumns.ID + " DESC");
			}

			@Override
			public void onLoadFinished(Loader<Cursor> paramLoader, Cursor paramD) {
				tweetCursorAdapter.swapCursor(paramD);

			}

			@Override
			public void onLoaderReset(Loader<Cursor> paramLoader) {
				tweetCursorAdapter.swapCursor(null);
			}
		});		
		
		
		return view;
	}
	
	@Override
	public void onListItemClick(ListView l, final View v, int position,
			final long id) {
		AlertDialog.Builder adb = new AlertDialog.Builder(getActivity());
		View view = getActivity().getLayoutInflater().inflate(
				R.layout.view_dialog_tweet_onclick, null);
		adb.setView(view);
		adb.setTitle(getString(R.string.what_to_do));
		final AlertDialog ad = adb.create();

		Button followLink = (Button) view.findViewById(R.id.btn_follow_link);
		Button retweet = (Button) view.findViewById(R.id.btn_retweet);
		Button deleteTweet = (Button) view.findViewById(R.id.btn_delete_tweet);

		Cursor cursor = (Cursor) l.getItemAtPosition(position);

		final int tweetUserId = Integer.parseInt(cursor.getString(cursor
				.getColumnIndex(TweetContract.TweetsColumns.USER_ID)));
		int currentSelectionUserId = Integer.parseInt(ContextHolder
				.getInstance()
				.getContext()
				.getSharedPreferences(getString(R.string.user_details),
						Context.MODE_PRIVATE)
				.getString(getString(R.string.user_id), ""));
		if (tweetUserId == currentSelectionUserId) {
			deleteTweet.setVisibility(View.VISIBLE);
			retweet.setVisibility(View.GONE);
		}

		final String link = cursor.getString(cursor
				.getColumnIndex(TweetContract.TweetsColumns.LINK));

		if (link != null) {
			followLink.setVisibility(View.VISIBLE);
		}

		View.OnClickListener onClickListener = new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent;
				HttpRequestBuilder httpRequestBuilder;
				NotificationCompat.Builder nb;
				NotificationManager nm;
				switch (v.getId()) {
				case R.id.btn_follow_link:
					intent = new Intent(SearchResultTweetsFragment.this.getActivity(),
							WebActivity.class);
					intent.putExtra(getString(R.string.link), link);
					SearchResultTweetsFragment.this.getActivity().startActivity(intent);
					ad.dismiss();
					break;
				case R.id.btn_retweet:
					intent = new Intent(SearchResultTweetsFragment.this.getActivity(),
							HttpService.class);
					httpRequestBuilder = new HttpRequestBuilder();
					httpRequestBuilder.setType(HttpRequestBuilder.Type.POST);
					httpRequestBuilder
							.setTwitterRequest(HttpRequestBuilder.TwitterRequest.STATUSES_RETWEET);
					httpRequestBuilder
							.setUrl(TwitterAPI.URL.TWITTER_STATUSES_RETWEET_URL
									+ String.valueOf(id) + getString(R.string.json_format));
					intent.putExtra(HttpService.HTTP_REQUEST_BUILDER,
							httpRequestBuilder);
					SearchResultTweetsFragment.this.getActivity().startService(intent);
					nb = new NotificationCompat.Builder(ContextHolder
							.getInstance().getContext())
							.setSmallIcon(android.R.drawable.star_on)
							.setContentText(getString(R.string.retweeting_in_process))
							.setTicker(getString(R.string.retweeting_in_process));
					nm = (NotificationManager) ContextHolder.getInstance()
							.getContext()
							.getSystemService(Context.NOTIFICATION_SERVICE);
					nm.notify(NotificationsHelper.RETWEET, nb.build());
					nm.cancel(NotificationsHelper.RETWEET);
					ad.dismiss();
					break;
				case R.id.btn_delete_tweet:
					intent = new Intent(SearchResultTweetsFragment.this.getActivity(),
							HttpService.class);
					httpRequestBuilder = new HttpRequestBuilder();
					httpRequestBuilder.setType(HttpRequestBuilder.Type.POST);
					httpRequestBuilder
							.setTwitterRequest(HttpRequestBuilder.TwitterRequest.STATUSES_DESTROY);
					httpRequestBuilder
							.setUrl(TwitterAPI.URL.TWITTER_STATUSES_DESTROY_URL
									+ String.valueOf(id) + getString(R.string.json_format));
					intent.putExtra(HttpService.HTTP_REQUEST_BUILDER,
							httpRequestBuilder);
					SearchResultTweetsFragment.this.getActivity().startService(intent);
					nb = new NotificationCompat.Builder(ContextHolder
							.getInstance().getContext())
							.setSmallIcon(android.R.drawable.star_on)
							.setContentText(getString(R.string.deleting_in_process))
							.setTicker(getString(R.string.deleting_in_process));
					nm = (NotificationManager) ContextHolder.getInstance()
							.getContext()
							.getSystemService(Context.NOTIFICATION_SERVICE);
					nm.notify(NotificationsHelper.DELETE_TWEET, nb.build());
					nm.cancel(NotificationsHelper.DELETE_TWEET);
					ad.dismiss();
					break;
				}
			}
		};
		followLink.setOnClickListener(onClickListener);
		retweet.setOnClickListener(onClickListener);
		deleteTweet.setOnClickListener(onClickListener);
		ad.show();
	}

}
